home *** CD-ROM | disk | FTP | other *** search
- unit KeysU;
-
- interface
-
- procedure PressKey(Key: Char);
- procedure ReleaseKey(Key: Char);
- procedure SendKeys(const Keys: String);
-
- const
- SnapShotWholeScreen: Boolean = False;
-
- implementation
-
- uses
- WinTypes, WinProcs, Forms, SysUtils;
-
- const
- KeyEventF_KeyDown = 0;
- {$ifndef WIN32}
- KeyEventF_KeyUp = $80; {It changes to 2 in Win32}
-
- procedure Keybd_Event; far; external 'USER' index 289;
-
- procedure PostVirtualKeyEvent(vk: Word; fUp: Boolean);
- var
- AXReg, BXReg: WordRec;
- const
- ButtonUp: array[Boolean] of Byte = (KeyEventF_KeyDown, KeyEventF_KeyUp);
- begin
- AXReg.Hi := ButtonUp[fUp];
- AXReg.Lo := vk;
- BXReg.Hi := 0; { not an extended scan code }
- { Special processing for the PrintScreen key. }
- { If scan code is set to 1 it copies entire }
- { screen. If set to 0 it copies active window. }
- if vk = vk_SnapShot then
- BXReg.Lo := Byte(SnapShotWholeScreen)
- else
- BXReg.Lo := MapVirtualKey(vk, 0);
- asm
- mov ax, AXReg
- mov bx, BXReg
- call Keybd_Event
- end;
- end;
- {$else}
- procedure PostVirtualKeyEvent(vk: Word; fUp: Boolean);
- var
- ScanCode: Byte;
- const
- ButtonUp: array[Boolean] of Byte = (KeyEventF_KeyDown, KeyEventF_KeyUp);
- begin
- if vk = vk_SnapShot then
- { Special processing for the PrintScreen key. }
- { If scan code is set to 1 it copies entire }
- { screen. If set to 0 it copies active window. }
- ScanCode := Byte(SnapShotWholeScreen)
- else
- ScanCode := MapVirtualKey(vk, 0);
- Keybd_Event(vk, ScanCode, ButtonUp[fUp], 0);
- end;
- {$endif}
-
- procedure PressKey(Key: Char);
- begin
- PostVirtualKeyEvent(Ord(Key), False)
- end;
-
- procedure ReleaseKey(Key: Char);
- begin
- PostVirtualKeyEvent(Ord(Key), True)
- end;
-
- procedure SendKeys(const Keys: String);
- var
- Loop: Byte;
- begin
- for Loop := 1 to Length(Keys) do
- begin
- PostVirtualKeyEvent(Ord(Keys[Loop]), False); { Press key }
- PostVirtualKeyEvent(Ord(Keys[Loop]), True); { Release key }
- end;
- { Let the keys be processed }
- Application.ProcessMessages;
- end;
-
- end.
-